﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using monitor_2_DBwork;

namespace monitor_server_2_WPF.DeviceListParamPanels
{
    /// <summary>
    /// Логика взаимодействия для RegionParamPanel.xaml
    /// </summary>
    public partial class RegionParamPanel : UserControl
    {
        private static DeviceListTabControl.UpdateRegionTreeNodeDelegate _updateRegionTreeNode = DeviceListTabControl.UpdateRegion;

        private RegionParamPanel()
        {
            InitializeComponent();
            MainStationLabel.Content = Res.Rm.GetString("HeadStation");
            StationsBox.ItemsSource = RegionStations;
            AcceptButton.Content = new MyDockPanel(Res.GetBitMapFromPng("accept_image_16"), Res.Rm.GetString("Accept"));
            AddStationExpander.Header = Res.Rm.GetString("AddRegion");
        }

        private static RegionParamPanel _panel;
        private static readonly ObservableCollection<String> RegionStations = new ObservableCollection<string>();
        private string _currentStation;


        public static UIElement GetPanel(DeviceTreeItem.RegionInfo info)
        {
            if(_panel == null)
                _panel = new RegionParamPanel();
            _panel.ToOrigin();
            _panel.RegionNameLabel.Content = info.Region;
            _panel._currentStation = info.MainStation;
            UpdateRegionStations(info.Region);
            foreach (var item in _panel.StationsBox.Items)
            {
                if (item.Equals(info.MainStation))
                    _panel.StationsBox.SelectedItem = item;
            }
            if (info.MainStation == null)
                _panel.AcceptButton.IsEnabled = false;
            return _panel;
        }

        public static void UpdateRegionStations(string regionName)
        {
            var list = DBwork.GetRegionStationsList(regionName);
            RegionStations.Clear();
            foreach (var station in list)
            {
                RegionStations.Add(station);
            }
        }

        private void StationsBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var box = sender as ComboBox;
            if (box.SelectedItem == null)
            {
                AcceptButton.IsEnabled = false;
            }
            else
            {
                AcceptButton.IsEnabled = _currentStation == null || !box.SelectedItem.Equals(_currentStation);
            }
            
        }

        private void AcceptButton_OnClick(object sender, RoutedEventArgs e)
        {
            var chk = DBwork.UpdateData("region", new[] {RegionNameLabel.Content.ToString(), StationsBox.SelectedItem.ToString()});
            _updateRegionTreeNode();
            if (chk)
                MessageBox.Show(Res.Rm.GetString("Complete"));
        }

        private void ToOrigin()
        {
            AddStationPanelGrid.Children.Clear();
            AddStationPanelGrid.Visibility = Visibility.Collapsed;
            AddStationExpander.IsExpanded = false;
            AddStationExpander.Visibility = Visibility.Visible;
        }

        private void AddStationExpander_OnExpanded(object sender, RoutedEventArgs e)
        {
            AddStationExpander.Visibility = Visibility.Collapsed;
            AddStationPanelGrid.Visibility = Visibility.Visible;
            AddStationPanelGrid.Children.Add(AddStationPanel.GetPanel(RegionNameLabel.Content.ToString()));
            //var h = ((AddStationPanel) AddStationPanelGrid.Children[0]).Height;
            //var a = new DoubleAnimation(0, 1, new Duration(new TimeSpan(0, 0, 0, 500)));
            //AddStationPanelGrid.Children[0].BeginAnimation(OpacityProperty, a);
        }
    }
}
